﻿/**********************************************************************************
 * Project Name :   
 * Remarks      :  
 * Class Name   :  XmlData 
 * Version      :  v1.0.0.0 
 * Author       :  Joey Liang 
 * Description  : 
 * Created Time :  2013/8/14 22:36:26
 * Update Man   :  Joey Liang
 * Update Time  :  2013/8/14 22:36:26
************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace JoeyLibs.Utility.Xml
{
    public class XmlData
    {
        public XmlData(string xmlPath)
        {
            this.xmlPath = xmlPath;
            this.xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlPath);
        }

        #region Fields & Event

        private string xmlPath;

        private XmlDocument xmlDoc;

        #endregion

        #region Properties

        #endregion

        #region Methods

        public XmlNode GetNode(string xpath)
        {
            return xmlDoc.SelectSingleNode(xpath);
        }

        public XmlNode GetNode(string xpath, Func<XmlNode, bool> func)
        {
            XmlNodeList list = GetNodeList(xpath);
            return list.Find(func);
        }

        public XmlNodeList GetNodeList(string xpath)
        {
            return xmlDoc.SelectNodes(xpath);
        }

        public ICollection<XmlNode> GetNodeList(string xpath, Func<XmlNode, bool> func)
        {
            XmlNodeList list = GetNodeList(xpath);
            return list.FindAll(func);
        }

        public XmlNode NewNode(string nodeName, XmlNodeType nodeType = XmlNodeType.Element)
        {
            return xmlDoc.CreateNode(nodeType, nodeName, string.Empty);
        }

        public void SetNodeValue(string xpath, string value)
        {
            XmlNode node = GetNode(xpath);
            node.Value = value;
        }

        public void SetNodeAttribute(string xpath, string attribute, string value)
        {
            XmlNode node = GetNode(xpath);
            node.Attributes[attribute].Value = value;
        }

        public void AddNode(string xpathParent, XmlNode node)
        {
            XmlNode parentNode = GetNode(xpathParent);
            AddNode(parentNode, node);
        }

        public void AddNode(XmlNode parentNode, XmlNode node)
        {
            if (parentNode != null && node != null)
                parentNode.AppendChild(node);
        }

        public XmlNode AddAttribute(XmlNode node, string attrName, string attrValue)
        {
            XmlAttribute nameAttr = NewNode(attrName, XmlNodeType.Attribute) as XmlAttribute;
            nameAttr.Value = attrValue;
            node.Attributes.Append(nameAttr);

            return node;
        }

        public void SaveChanges()
        {
            SaveToNewFile(this.xmlPath);
        }

        public void SaveToNewFile(string path)
        {
            xmlDoc.Save(path);
        }

        #endregion
    }
}
